﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WD.Classes;
using WDutils;
using System.Globalization;

namespace WD
{
    public partial class ExportFrm : Form
    {
        public ExportFrm()
        {
            InitializeComponent();
        }

        private int weekNumber(DateTime fromDate)
        {
            // Get jan 1st of the year
            DateTime startOfYear = fromDate.AddDays(-fromDate.Day + 1).AddMonths(-fromDate.Month + 1);
            // Get dec 31st of the year
            DateTime endOfYear = startOfYear.AddYears(1).AddDays(-1);
            // ISO 8601 weeks start with Monday 
            // The first week of a year includes the first Thursday 
            // DayOfWeek returns 0 for sunday up to 6 for saterday
            int[] iso8601Correction = { 6, 7, 8, 9, 10, 4, 5 };
            int nds = fromDate.Subtract(startOfYear).Days + iso8601Correction[(int)startOfYear.DayOfWeek];
            int wk = nds / 7;
            switch (wk)
            {
                case 0:
                    // Return weeknumber of dec 31st of the previous year
                    return weekNumber(startOfYear.AddDays(-1));
                case 53:
                    // If dec 31st falls before thursday it is week 01 of next year
                    if (endOfYear.DayOfWeek < DayOfWeek.Thursday)
                        return 1;
                    else
                        return wk;
                default: return wk;
            }
        }

        private void ExportFrm_Load(object sender, EventArgs e)
        {
            DTP_Filter_From.Value = DateTime.Now;
            DTP_Filter_Untill.Value = DateTime.Now;
        }

        private void B_Save_Export_Click(object sender, EventArgs e)
        {
            SFD_Export.ShowDialog();
            if(SFD_Export.FileName == "") return;
            System.IO.StreamWriter objSWFileExport = new System.IO.StreamWriter(SFD_Export.FileName, false);
        
            //Brind data from set time-interval and print it nicely.

            ActivityLogEvents.GetActivityLogCollection evGetActivities = new ActivityLogEvents.GetActivityLogCollection();


            long longTimeStamp_BeginningOfDay = Utils.Unix.GetUnixTimestampSecondsFromDateTime(Utils.Unix.GetDateTime_BeginningOfDay(DTP_Filter_From.Value));
            long longTimeStamp_EndOfDay = Utils.Unix.GetUnixTimestampSecondsFromDateTime(Utils.Unix.GetDateTime_EndOfDay(DTP_Filter_Untill.Value));

            evGetActivities.setTimeFilter_Start(longTimeStamp_BeginningOfDay);
            evGetActivities.setTimeFilter_End(longTimeStamp_EndOfDay);
            
            List<ActivityLog> listActivityLogs4Export = new List<ActivityLog>();

            listActivityLogs4Export = (List<ActivityLog>)(evGetActivities.Request());

            List<ActivityLog> listActivityLogs4Export_Sorted = listActivityLogs4Export.OrderBy(activity => activity.getCreatedOn()).ToList();

            DateTime dtPreviousActivity = new DateTime();

            foreach (ActivityLog objAL in listActivityLogs4Export_Sorted)
            {
                bool boolIsWeekEnd = (objAL.getCreatedOnDateTime().DayOfWeek == DayOfWeek.Saturday || objAL.getCreatedOnDateTime().DayOfWeek == DayOfWeek.Sunday);
                if (!CB_ExportWeekends.Checked)
                    if (boolIsWeekEnd) 
                        continue;

                

                if (weekNumber(objAL.getCreatedOnDateTime()) == weekNumber(dtPreviousActivity))
                {
                    //in the same week
                    if (objAL.getCreatedOnDateTime().Day != dtPreviousActivity.Day)
                    {
                        objSWFileExport.WriteLine("--------- Day : " + objAL.getCreatedOnDateTime().ToLongDateString() + " --------");
                    }
                }
                else
                {
                    objSWFileExport.WriteLine("");
                    objSWFileExport.WriteLine("------------------<<< Week : " + weekNumber(objAL.getCreatedOnDateTime()).ToString() + " >>>------------------");
                    if (objAL.getCreatedOnDateTime().Day != dtPreviousActivity.Day)
                    {
                        objSWFileExport.WriteLine("--------- Day : " + objAL.getCreatedOnDateTime().ToLongDateString() + " --------");
                    }
                }

                objSWFileExport.WriteLine((boolIsWeekEnd ? "*" : "") + "[" + objAL.getCreatedOnDateTime().ToShortDateString() + " "
                    + objAL.getCreatedOnDateTime().ToShortTimeString() + "] "
                    + objAL.getText());

                dtPreviousActivity = objAL.getCreatedOnDateTime();
            }

            

            objSWFileExport.Close();
            MessageBox.Show("Export successfuly saved.", "Export saved", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void ExportFrm_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            this.Hide();
        }
    }
}
